package thread_base.thread_safe;

import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReference;

// *
// Written by MAX 2024/7/25 9:45
// Note:*
public class UnsafeCachingFactorizer {

    private final AtomicReference<BigInteger> lastNumber = new AtomicReference<>();
    private final AtomicReference<BigInteger[]> lastFactors = new AtomicReference<>();

    public void service(ServletRequest req, ServletResponse resp) {
        BigInteger i = extractFromRequest(req);
        if (i.equals(lastNumber.get())) {
            encodeIntoResponse(resp, lastFactors.get());

        }else {
            BigInteger[] factors = factor(i);
            lastNumber.set(i);
            lastFactors.set(factors);
            encodeIntoResponse(resp, factors);
        }

    }

    BigInteger[] factor(BigInteger i) {
        // Doesn't really factor
        return new BigInteger[]{i};
    }

    BigInteger extractFromRequest(ServletRequest req) {
        return new BigInteger("7");
    }

    void encodeIntoResponse(ServletResponse resp, BigInteger[] factors) {
    }
}
